package com.shambles.manage2.service.impl;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shambles.manage2.service.AbattoirService;
import com.shambles.manage2.tkmapper.entity.*;
import com.shambles.manage2.tkmapper.mapper.*;
import com.shambles.manage2.utils.CommonUtils;
import com.shambles.manage2.utils.MapUtils;
import com.shambles.manage2.utils.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.weekend.Weekend;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AbattoirServiceImpl implements AbattoirService {
    @Autowired
    private SysLogMapper sysLogMapper;
    @Autowired
    private AbattoirBoothMapper abattoirBoothMapper;
    @Autowired
    AdmissionRegisterMapper admissionRegisterMapper;
    @Autowired
    private AbattoirBoothDetailMapper abattoirBoothDetailMapper;
    @Autowired
    private SubscribeDetailMapper subscribeDetailMapper;
    @Autowired
    private SubscribeInformationMapper subscribeInformationMapper;

    @Override
    public List<AbattoirBooth> abattoirList() {
        return abattoirBoothMapper.selectAll();
    }

    @Override
    public void updateAbattoir(AbattoirBooth abattoirBooth) {
        abattoirBoothMapper.updateByPrimaryKeySelective(abattoirBooth);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void allot(AdmissionRegister admissionRegister) {
        String aminGUID = IdUtil.simpleUUID();
        admissionRegister.setGuid(aminGUID);
        admissionRegister.setOperationPersonnelGuid(CommonUtils.getCurrentUserGuid());
        admissionRegister.setOperationPersonnelName(CommonUtils.getCurrentUserName());
        admissionRegister.setAdmissionTime(System.currentTimeMillis());
        admissionRegisterMapper.insertSelective(admissionRegister);
        SubscribeDetail condition = new SubscribeDetail();
        condition.setSubscribeGuid(admissionRegister.getSubscribeGuid());
        List<SubscribeDetail> subscribeDetailList = subscribeDetailMapper.select(condition);
        ArrayList<AbattoirBoothDetail> abattoirBoothDetailList = new ArrayList<>();
        for (SubscribeDetail detail : subscribeDetailList) {
            AbattoirBoothDetail boothDetail = new AbattoirBoothDetail();
            BeanUtils.copyProperties(detail, boothDetail);
            boothDetail.setGuid(IdUtil.simpleUUID());
            boothDetail.setBoothGuid(admissionRegister.getBoothGuid());
            boothDetail.setAdmissionGuid(aminGUID);
            boothDetail.setSubscribeGuid(admissionRegister.getSubscribeGuid());
            boothDetail.setCattleGuid(detail.getCattleGuid());
            abattoirBoothDetailList.add(boothDetail);
        }
        abattoirBoothDetailMapper.insertList(abattoirBoothDetailList);
        subscribeInformationMapper.updateState(3, admissionRegister.getSubscribeGuid());
        abattoirBoothMapper.updateState(1, admissionRegister.getBoothGuid());
        //添加系统日志
        SysLog sysLog = new SysLog();
        sysLog.setGuid(IdUtil.simpleUUID());
        sysLog.setOperationPeople(CommonUtils.getCurrentUserName());
        sysLog.setFunctionName("入场");
        sysLog.setContent(CommonUtils.getCurrentUserName() + "操作入场功能");
        sysLog.setOperationTime(System.currentTimeMillis());
        this.sysLogMapper.insertSelective(sysLog);
    }

    /**
     * 新增摊位
     *
     * @param abattoirBooth
     */
    @Override
    public void insertAbattoirBooth(AbattoirBooth abattoirBooth) {
        AbattoirBooth insertAbattoirBooth = new AbattoirBooth();
        insertAbattoirBooth.setGuid(IdUtil.simpleUUID());
        insertAbattoirBooth.setStatus(1);
        insertAbattoirBooth.setManager(abattoirBooth.getManager());
        insertAbattoirBooth.setBoothName(abattoirBooth.getBoothName());
        insertAbattoirBooth.setMark(abattoirBooth.getMark());
        insertAbattoirBooth.setUser(abattoirBooth.getUser());

        this.abattoirBoothMapper.insertSelective(insertAbattoirBooth);
    }

    /**
     * 查询摊位明细
     */
    public List<AbattoirBoothDetail> queryBoothDetail(String boothGuid) {
        return this.abattoirBoothDetailMapper.queryBoothDetail(boothGuid);
    }

    /**
     * 根据入场guid查询摊位明细
     */
    public List<AbattoirBoothDetail> queryBoothDetailByGuid(String guid) {
        return this.abattoirBoothMapper.queryBoothDetailByGuid(guid);
    }

    /**
     * 删除摊位
     */
    public void deleteBooth(String guid) {
        this.abattoirBoothMapper.deleteBooth(guid);
    }

    /**
     * 修改摊位
     */
    public void updateBooth(AbattoirBooth abattoirBooth) {
        String Guid = abattoirBooth.getGuid();
        Weekend<AbattoirBooth> abattoirBoothWeekend = new Weekend<>(AbattoirBooth.class);
        abattoirBoothWeekend.weekendCriteria().andEqualTo(AbattoirBooth::getGuid, Guid);
        abattoirBooth.setGuid(null);
        this.abattoirBoothMapper.updateByExampleSelective(abattoirBooth, abattoirBoothWeekend);
    }

    /**
     * 查询摊位
     */
    public PageInfo<AbattoirBooth> queryAbattoirBooth(MessageRequestVM messageRequestVM) {
        PageInfo<AbattoirBooth> obattoirBoothPageInfo = PageHelper.startPage(messageRequestVM.getPageIndex(), messageRequestVM.getPageSize()).doSelectPageInfo(() ->
                abattoirBoothMapper.queryAbattoirBooth()
        );
        List<AbattoirBooth> list = obattoirBoothPageInfo.getList();
        List<AbattoirBoothDetail> abattoirBoothDetails = this.abattoirBoothMapper.querySlaughter();
        HashMap<String, List<AbattoirBoothDetail>> map = new HashMap<>();
        for (AbattoirBoothDetail detail : abattoirBoothDetails) {
            List<AbattoirBoothDetail> abattoirBoothDetails1 = map.get(detail.getBoothGuid());
            if (abattoirBoothDetails1 == null) {
                abattoirBoothDetails1 = new ArrayList<>();
                map.put(detail.getBoothGuid(), abattoirBoothDetails1);
            }
            abattoirBoothDetails1.add(detail);
        }
        for (AbattoirBooth abattoirBooth : list) {
            //获取摊位明细信息
            abattoirBooth.abattoirBoothDetails = map.get(abattoirBooth.getGuid());
            //获取摊位用户及总数量
            abattoirBooth.userMap = this.abattoirBoothMapper.queryUser(abattoirBooth.getGuid());
            //获取相同人员的数量
            List<Map<String, Object>> userMap = abattoirBooth.userMap;
            HashMap<String, Integer> hashMap = new HashMap<>();
            for (Map<String, Object> item : userMap) {
                String phone = MapUtils.getString("phone", item);
                Integer total = MapUtils.getInteger(phone, hashMap);
                if (ObjectUtils.isEmpty(total)) {
                    total = 0;
                }
                total++;
                hashMap.put(phone, total);
            }
            userMap.forEach(e -> e.put("total", hashMap.get(e.get("phone"))));
//            for (Map<String, Object> objectMap : userMap) {
//                objectMap.put("total", hashMap.get(objectMap.get("phone")));
//            }
        }
        return obattoirBoothPageInfo;
    }

    /**
     * 统计摊位明细
     */
    public int countByBoothGuid(String boothGuid) {
        return this.abattoirBoothDetailMapper.countByBoothGuid(boothGuid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancelAllot(AdmissionRegister admissionRegister) {

        admissionRegisterMapper.deleteByGuid(admissionRegister.getGuid());

        abattoirBoothDetailMapper.deleteByAdmissionGuid(admissionRegister.getGuid());
        abattoirBoothMapper.updateState(0, admissionRegister.getBoothGuid());
    }

    @Override
    public PageInfo<AdmissionRegister> admissionList(MessageRequestVM messageRequestVM, Integer status) {
        ISelect select = () -> admissionRegisterMapper.selectByStatus(status);
        if (status == null) {
            select = () -> admissionRegisterMapper.selectAll();
        }
        PageInfo<AdmissionRegister> admissionRegisterPageInfo = PageHelper.startPage(messageRequestVM.getPageIndex(), messageRequestVM.getPageSize()).doSelectPageInfo(
                select
        );
        List<AdmissionRegister> list = admissionRegisterPageInfo.getList();
        for (AdmissionRegister admissionRegister : list) {
            admissionRegister.abattoirBoothDetailList = abattoirBoothDetailMapper.selectByAdmissionGuid(admissionRegister.getGuid());
        }
        return admissionRegisterPageInfo;
    }


}
